/*============================================================================*/

* Do: Preliminar regressions
* Last update: 09-05-2023

/*============================================================================*/

* ==============================================================================
* SECTION: FOLDERS
* ==============================================================================

*------------------------ SUBFOLDERS (DO NOT CHANGE)
gl in "$root/Final"
gl analysis "$root/Analysis"

cap log close

log using regressions_part1, replace

use "$in/data_regressions_enpove", clear

* ==============================================================================
* SECTION: REGRESSIONS
* ==============================================================================


*------------------------ MAIN (Outcome = discrim_P701)
* Define specifications
local indvchar age_P205_A lntimeinperu_months ib1.education_group marital_status i.hh_sec hhsize sharebedroom_P317 

local community1 lninformemprate_ubigeo17 /* relinformal lnvzimmigr_ccpp17 */

local community2 lnpopulation_ubigeo17 log_vzs_07 lnhhexpc_ubigeo lnagriculture_rate_07 lnmanufactoring_rate_07 log_duration_Lima 

local work totincome_1000 formal i.occ1dig

local source i.emigp_m

local iv changeexport_log /* log_vzs_07 */


* Descriptives

su discrim_P701 discrimplace1_P702_1 discrimplace5_P702_5 discrimplace6_P702_6 `community1' `community2' `indvchar'  `work' `iv', sep(0)

forv i=0/2 {

if `i'~=2 {
local sex "if female==`i'"
}

else {
local sex "female"
}
	reg discrim_P701 `community1' `community2' `indvchar' `sex', vce(cluster ubigeo) 
		qui sum discrim_P701 if e(sample) == 1 
		local mendep = r(mean)
		outreg2 using "$analysis\MainOLS`i'.xls", dec(3) nocons replace label ///
			addstat("Mean dep. var", `mendep') addtext(Origin Municipality FE, No) 

	reg discrim_P701 `community1' `community2' `indvchar' `work' `sex', vce(cluster ubigeo)
		qui sum discrim_P701 if e(sample) == 1 
		local mendep = r(mean)
		outreg2 using "$analysis\MainOLS`i'.xls", dec(3) nocons append label ///
			addstat("Mean dep. var", `mendep') addtext(Origin Municipality FE, No)

	reg discrim_P701 `community1' `community2' `indvchar'  `work' `source' `sex', vce(cluster ubigeo)
		qui sum discrim_P701 if e(sample) == 1 
		local mendep = r(mean)
		outreg2 using "$analysis\MainOLS`i'.xls", dec(3) drop(*emi*) nocons append label ///
			addstat("Mean dep. var", `mendep') addtext(Origin Municipality FE, Yes)

	reg  `community1' `iv' `community2' `indvchar' `work' `source' `sex', vce(cluster ubigeo)
		qui sum `community1' if e(sample) == 1 
		local mendep = r(mean)
		outreg2 using "$analysis\MainOLS`i'.xls", dec(3) drop(*emi*) nocons append label ///
			addstat("Mean dep. var", `mendep') addtext(First Stage Network)

	ivreg2  discrim_P701 (`community1' = `iv') `community2' `indvchar' `work' `source' `sex', partial(`source') cluster(ubigeo)
		qui sum discrim_P701 if e(sample) == 1 
		local mendep = r(mean)
		outreg2 using "$analysis\MainOLS`i'.xls", dec(3) drop(*emi*) nocons append label ///
			addstat("Mean dep. var", `mendep')  addtext(Origin Municipality FE, Yes)

}


*------------------------ PLACES OF DISCRIMINATION

forv i=0/2 {

if `i'~=2 {
local sex "if female==`i'"
local sex2 "if employed & female==`i'"
}

else {
local sex "female"
local sex2 "female if employed"
}

	reg discrimplace1_P702_1 `community1' `community2' `indvchar'  `work' `source' `sex2', vce(cluster ubigeo)
		qui sum discrimplace1_P702_1 if e(sample) == 1 
		local mendep = r(mean)
		outreg2 using "$analysis\MainExtra`i'.xls", dec(3) drop(*emigp_m) nocons replace label addstat("Mean dep. var", `mendep')

	ivreg2  discrimplace1_P702_1 (`community1' = `iv') `indvchar' `community2' `work' `source' `sex2', cluster(ubigeo)
		qui sum discrimplace1_P702_1 if e(sample) == 1 
		local mendep = r(mean)
		outreg2 using "$analysis\MainExtra`i'.xls", dec(3) drop(*emigp_m) nocons append label addstat("Mean dep. var", `mendep') 

	reg discrimplace5_P702_5 `community1' `community2' `indvchar'  `work' `source' `sex', vce(cluster ubigeo)
		qui sum discrimplace5_P702_5 if e(sample) == 1 
		local mendep = r(mean)
		outreg2 using "$analysis\MainExtra`i'.xls", dec(3) drop(*emigp_m) nocons append label addstat("Mean dep. var", `mendep')

	ivreg2  discrimplace5_P702_5 (`community1' = `iv') `indvchar' `community2' `work' `source' `sex', cluster(ubigeo)
		qui sum discrimplace5_P702_5 if e(sample) == 1 
		local mendep = r(mean)
		outreg2 using "$analysis\MainExtra`i'.xls", dec(3) drop(*emigp_m) nocons append label addstat("Mean dep. var", `mendep') 

	reg discrimplace6_P702_6 `community1' `community2' `indvchar'  `work' `source' `sex', vce(cluster ubigeo)
		qui sum discrimplace6_P702_6 if e(sample) == 1 
		local mendep = r(mean)
		outreg2 using "$analysis\MainExtra`i'.xls", dec(3) drop(*emigp_m) nocons append label addstat("Mean dep. var", `mendep')

	ivreg2  discrimplace6_P702_6 (`community1' = `iv') `indvchar' `community2' `work' `source' `sex', cluster(ubigeo)
		qui sum discrimplace6_P702_6 if e(sample) == 1 
		local mendep = r(mean)
		outreg2 using "$analysis\MainExtra`i'.xls", dec(3) drop(*emigp_m) nocons append label addstat("Mean dep. var", `mendep') 

}

** employment and earnings

local outcomes employed lntotincome_ingtot

foreach dv of local outcomes {

forv i=0/2 {

if `i'~=2 {
local sex "if female==`i'"
}

else {
local sex "female"
}


       reg `dv' `indvchar' `sex', vce(cluster ubigeo) 
		qui sum discrim_P701 if e(sample) == 1 
		local mendep = r(mean)
		outreg2 using "$analysis\empl_`dv'`i'.xls", dec(3) nocons replace label ///
			addstat("Mean dep. var", `mendep') addtext(Origin Municipality FE, No) 

	reg `dv' `community1' `community2' `indvchar'  `sex', vce(cluster ubigeo) 
		qui sum discrim_P701 if e(sample) == 1 
		local mendep = r(mean)
		outreg2 using "$analysis\empl_`dv'`i'.xls", dec(3) nocons append label ///
			addstat("Mean dep. var", `mendep') addtext(Origin Municipality FE, No) 

	reg `dv' `community1' `community2' `indvchar'  `source' `sex', vce(cluster ubigeo)
		qui sum discrim_P701 if e(sample) == 1 
		local mendep = r(mean)
		outreg2 using "$analysis\empl_`dv'`i'.xls", dec(3) drop(*emigp_m) nocons append label ///
			addstat("Mean dep. var", `mendep') addtext(Origin Municipality FE, Yes)

	ivreg2  `dv' (`community1' = `iv') `community2'  `indvchar' `source' `sex', cluster(ubigeo)
		qui sum discrim_P701 if e(sample) == 1 
		local mendep = r(mean)
		outreg2 using "$analysis\empl_`dv'`i'.xls", dec(3) drop(*emigp_m) nocons append label ///
			addstat("Mean dep. var", `mendep')  addtext(Origin Municipality FE, Yes)

}
}


log close
